package com.hst.web.base.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.hst.core.ServiceContext;
import com.hst.core.dao.IORDao;
import com.hst.core.dao.ISQLDao;
import com.hst.core.dao.ORQuery;
import com.hst.core.dao.ORQuery.Op;
import com.hst.core.json.JsonUtil;
import com.hst.core.meta.MetaRepository;
import com.hst.web.auth.po.User;
import com.hst.web.base.entity.TCompanyEmployee;
import com.hst.web.base.entity.TPersoneltransferInfo;
import com.hst.web.base.service.EmployeeService;
import com.hst.web.base.ui.CompanyEmployeeReview;

@Controller
@RequestMapping("/person")
public class PersonController {

	@Autowired
	IORDao dao;

	@Autowired
	MetaRepository mr;
	
	@Autowired
	ISQLDao sql;

	@Autowired
	EmployeeService service;

	/**
	 * 转移状态:待审核
	 */
	private static final String TRANS_STAUTS_APPLY = "00";

	/**
	 * 通行证申请状态：申请中
	 */
	private static final String CHECKT_STAUTS_APPLY = "1";
	
	@SuppressWarnings({ "unchecked" })
	private Map<String, Object> parseQuerys(String data) throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> querys = JsonUtil.read(data, Map.class);
		return querys;
	}

	/**
	 * 待审批的调动申请数量
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/transfer-existed")
	public void transferExisted(@RequestParam(required = false) String employeeCode,
			@RequestBody(required = false) List<String> employeeList, Model m) throws Exception {
		List<ORQuery> querys = new ArrayList<ORQuery>();
		if (employeeCode != null)
			querys.add(new ORQuery(Op.eq, "employeeCode", employeeCode));
		else if (employeeList != null && employeeList.size() > 0) {
			querys.add(new ORQuery(Op.in, "employeeCode", employeeList));
		} else {
			throw new Exception("employeecode is null");
		}
		querys.add(new ORQuery(Op.eq, "transStatus", TRANS_STAUTS_APPLY));
		m.addAttribute("active_apply", dao.list(TPersoneltransferInfo.class, querys).size());
	}

	/**
	 * 审批中的通行证申请数量
	 */
	@RequestMapping("/card-apply-existed")
	public void cardApplyExisted(String employeeCode, Model m) {
		List<ORQuery> querys = new ArrayList<ORQuery>();
		querys.add(new ORQuery(Op.eq, "employeeCode", employeeCode));
		querys.add(new ORQuery(Op.eq, "checkStatus", CHECKT_STAUTS_APPLY));
		m.addAttribute("active_apply", dao.list(TCompanyEmployee.class, querys).size());
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("/review-approve")
	public void reviewApprove(@RequestBody String data, Model m)
			throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> map = JsonUtil.read(data, Map.class);
		String s1 = (String) map.get("list");
		List<CompanyEmployeeReview> list = JsonUtil.readList(s1, CompanyEmployeeReview.class);
		String remark = (String) map.get("remark");
		service.reviewApprove(list, remark);
		m.addAttribute("data", "0");
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("/review-reject")
	public void reviewReject(@RequestBody String data, Model m)
			throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> map = JsonUtil.read(data, Map.class);
		String s1 = (String) map.get("list");
		List<CompanyEmployeeReview> list = JsonUtil.readList(s1, CompanyEmployeeReview.class);
		String remark = (String) map.get("remark");
		service.reviewReject(list, remark);
		m.addAttribute("data", "0");
	}
	
	@RequestMapping("/indexCount")
	public void indexCount(Model m) {
		User u = (User) ServiceContext.getInstance().getUser();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("companyCode", u.getCompanyCode());
		m.addAttribute("number1",sql.query("person.countCompanyEmployee", map));
		m.addAttribute("number2",sql.query("person.countIdeEmployee", map));
		m.addAttribute("number3",sql.query("person.countReviewEmployee", map));
		m.addAttribute("number4",sql.query("person.countCheckEmployee", map));
		m.addAttribute("number5",sql.query("person.countCheckedEmployee", map));
	}
	
	@RequestMapping("/getEmployeeName")
	public void getEmployeeName(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> params = parseQuerys(data);
		String employeeCode = (String) params.get("name");
		TCompanyEmployee employee = dao.get(TCompanyEmployee.class, employeeCode);
		m.addAttribute("name",employee.getFullname());
	}
	
	@RequestMapping("/getEmployee")
	public void getEmployee(@RequestBody String data, Model m) throws JsonGenerationException, JsonMappingException, IOException {
		Map<String, Object> params = parseQuerys(data);
		String employeeCode = (String) params.get("employeeCode");
		TCompanyEmployee employee = dao.get(TCompanyEmployee.class, employeeCode);
		m.addAttribute("employee",JsonUtil.toString(employee));
	}
}
